<template>
  <div class="admin-dashboard">
    <div class="welcome-banner">
      <h2>欢迎回来，{{ userInfo.username }}</h2>
      <div class="last-login">上次登录：{{ lastLoginTime }}</div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <template #header>
            <div class="card-header">
              <el-icon><User /></el-icon>
              <span>用户统计</span>
            </div>
          </template>
          <div class="card-content">
            <h3>总用户数</h3>
            <p class="number">{{ stats.totalUsers }}</p>
            <div class="stat-trend">
              <span>较昨日</span>
              <span :class="stats.userTrend >= 0 ? 'up' : 'down'">
                {{ Math.abs(stats.userTrend) }}%
              </span>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <template #header>
            <div class="card-header">
              <el-icon><Van /></el-icon>
              <span>停车位统计</span>
            </div>
          </template>
          <div class="card-content">
            <h3>可用车位</h3>
            <p class="number">{{ stats.availableParking }}</p>
            <div class="stat-detail">总车位：{{ stats.totalParking }}</div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <template #header>
            <div class="card-header">
              <el-icon><UserFilled /></el-icon>
              <span>访客统计</span>
            </div>
          </template>
          <div class="card-content">
            <h3>今日访客</h3>
            <p class="number">{{ stats.todayVisitors }}</p>
            <div class="stat-detail">待审核：{{ stats.pendingVisitors }}</div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover" class="stat-card">
          <template #header>
            <div class="card-header">
              <el-icon><Tools /></el-icon>
              <span>维修统计</span>
            </div>
          </template>
          <div class="card-content">
            <h3>待处理维修</h3>
            <p class="number">{{ stats.pendingRepairs }}</p>
            <div class="stat-detail">今日新增：{{ stats.todayNewRepairs }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 快捷操作区 -->
    <el-row :gutter="20" class="action-row">
      <el-col :span="16">
        <el-card shadow="hover" class="quick-actions">
          <template #header>
            <div class="card-header">
              <el-icon><Operation /></el-icon>
              <span>快捷操作</span>
            </div>
          </template>
          <div class="action-grid">
            <el-button type="primary" @click="router.push('/admin/user-mgmt')">
              <el-icon><User /></el-icon>用户管理
            </el-button>
            <el-button type="success" @click="router.push('/admin/parking-mgmt')">
              <el-icon><Van /></el-icon>车位管理
            </el-button>
            <el-button type="warning" @click="router.push('/admin/fee-mgmt')">
              <el-icon><Wallet /></el-icon>费用管理
            </el-button>
            <el-button type="info" @click="router.push('/admin/repair-mgmt')">
              <el-icon><Tools /></el-icon>报修管理
            </el-button>
            <el-button type="danger" @click="router.push('/admin/visitor-approval')">
              <el-icon><UserFilled /></el-icon>访客审批
            </el-button>
          </div>
        </el-card>
      </el-col>

      <el-col :span="8">
        <el-card shadow="hover" class="system-info">
          <template #header>
            <div class="card-header">
              <el-icon><Monitor /></el-icon>
              <span>系统信息</span>
            </div>
          </template>
          <div class="info-list">
            <div class="info-item">
              <span class="label">系统版本</span>
              <span class="value">v1.0.0</span>
            </div>
            <div class="info-item">
              <span class="label">服务器状态</span>
              <el-tag size="small" type="success">运行中</el-tag>
            </div>
            <div class="info-item">
              <span class="label">最后更新</span>
              <span class="value">{{ lastUpdateTime }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
// 导入部分
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { User, Van, UserFilled, Tools, Operation, Monitor, Wallet } from '@element-plus/icons-vue'
import dayjs from 'dayjs'
import { 
  getUserCount, 
  getAllParkingSpots,
  getAvailableParkingSpots,
  getAllVisitors,
  getAllRepairRequests
} from '@/api/adminApi'

const router = useRouter()
const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
const lastLoginTime = ref(dayjs().format('YYYY-MM-DD HH:mm:ss'))
const lastUpdateTime = ref('2024-03-20 10:00:00')

// 统计数据
const stats = ref({
  totalUsers: 0,
  userTrend: 5.2,
  availableParking: 0,
  totalParking: 0,
  todayVisitors: 0,
  pendingVisitors: 0,
  pendingRepairs: 0,
  todayNewRepairs: 0
})

// 初始化数据
onMounted(async () => {
  try {
    // 获取用户总数
    const userCountRes = await getUserCount()
    if (userCountRes.code === 200) {
      stats.value.totalUsers = userCountRes.data
    }

    // 获取车位统计
    const [allParkingRes, availableParkingRes] = await Promise.all([
      getAllParkingSpots(),
      getAvailableParkingSpots()
    ])
    if (allParkingRes.code === 200 && availableParkingRes.code === 200) {
      stats.value.totalParking = allParkingRes.data.length
      stats.value.availableParking = availableParkingRes.data.length
    }

    // 获取访客统计
    const visitorsRes = await getAllVisitors()
    if (visitorsRes.code === 200) {
      const today = dayjs().format('YYYY-MM-DD')
      stats.value.todayVisitors = visitorsRes.data.filter(v => 
        dayjs(v.visitDate).format('YYYY-MM-DD') === today
      ).length
      stats.value.pendingVisitors = visitorsRes.data.filter(v => 
        v.status === '待审核'
      ).length
    }

    // 获取维修统计
    const repairRes = await getAllRepairRequests()
    if (repairRes.code === 200) {
      const today = dayjs().format('YYYY-MM-DD')
      stats.value.pendingRepairs = repairRes.data.filter(r => 
        r.status === '待处理'
      ).length
      stats.value.todayNewRepairs = repairRes.data.filter(r => 
        dayjs(r.createdAt).format('YYYY-MM-DD') === today
      ).length
    }

    // 用户增长趋势暂时保持固定值
    stats.value.userTrend = 5.2
  } catch (error) {
    console.error('获取数据失败:', error)
  }
})
</script>

<style scoped>
.admin-dashboard {
  padding: 20px;
}

.welcome-banner {
  margin-bottom: 24px;
}

.welcome-banner h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.last-login {
  margin-top: 8px;
  color: #909399;
  font-size: 14px;
}

.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-content {
  text-align: center;
}

.card-content h3 {
  color: #606266;
  font-size: 14px;
  margin: 0 0 10px;
}

.number {
  font-size: 28px;
  font-weight: bold;
  color: #409EFF;
  margin: 10px 0;
}

.stat-trend {
  font-size: 12px;
  color: #909399;
}

.stat-trend .up {
  color: #67C23A;
}

.stat-trend .down {
  color: #F56C6C;
}

.stat-detail {
  font-size: 12px;
  color: #909399;
}

.action-row {
  margin-bottom: 20px;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-item .label {
  color: #606266;
}

.info-item .value {
  color: #303133;
}
</style>